<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 座位调整（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>疫情期间课堂的座位进行了特殊的调整&#xff0c;不能出现两个同学紧挨着&#xff0c;必须隔至少一个空位。</p> 
<p>给你一个整数数组 desk 表示当前座位的占座情况&#xff0c;由若干 0 和 1 组成&#xff0c;其中 0 表示没有占位&#xff0c;1 表示占位。</p> 
<p>在不改变原有座位秩序情况下&#xff0c;还能安排坐几个人&#xff1f;</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行是个子数组表示作为占座情况&#xff0c;由若干 0 和 1 组成&#xff0c;其中 0 表示没有占位&#xff0c;1 表示占位</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出数值表示还能坐几个人</p> 
<p></p> 
<h4>备注</h4> 
<p> 1 ≤ desk.length ≤ 2 * 10^4</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">1,0,0,0,1</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">1</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">只有desk[2]的位置可以坐一个人</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>我的解题思路如下&#xff1a;</p> 
<p>将输入解析为一个整型数组desk&#xff0c;然后遍历每一个元素desk[i]&#xff1a;</p> 
<ul><li>如果 desk[i] &#61;&#61; 0&#xff0c;则说明desk[i]是一个空位&#xff0c;此时只需要检查desk[i]的左右两边也是空位&#xff0c;则说明desk[i]可以坐人&#xff0c;此时将desk[i]更新为1&#xff0c;表示坐人了&#xff0c;而判断左右是否为空位的逻辑如下&#xff1a;</li></ul> 
<ol><li>如果 i &#61;&#61; 0 || desk[i-1] &#61;&#61; 0&#xff0c;则说明desk[i]左边是空位&#xff0c;i &#61;&#61; 0 说明其左边没有其他座位了&#xff0c;也相当于空位。</li><li>如果 i &#61;&#61; desk.length - 1 || desk[i&#43;1] &#61;&#61; 0&#xff0c;则说明desk[i]右边也是空位&#xff0c;i &#61;&#61; desk.length - 1说明其右边没有其他座位了&#xff0c;也就相当于空位</li></ol> 
<ul><li>如果 desk[i] !&#61; 0&#xff0c;则说明desk[i]不是一个空位&#xff0c;坐不了人&#xff0c;且说明了下一个i&#43;1座位也不能坐人</li></ul> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    int[] desk &#61; Arrays.stream(sc.nextLine().split(&#34;,&#34;)).mapToInt(Integer::parseInt).toArray();
    System.out.println(getResult(desk));
  }

  public static int getResult(int[] desk) {
    int ans &#61; 0;

    for (int i &#61; 0; i &lt; desk.length; i&#43;&#43;) {
      if (desk[i] &#61;&#61; 0) {
        boolean isLeftEmpty &#61; i &#61;&#61; 0 || desk[i - 1] &#61;&#61; 0;
        boolean isRightEmpty &#61; i &#61;&#61; desk.length - 1 || desk[i &#43; 1] &#61;&#61; 0;
        if (isLeftEmpty &amp;&amp; isRightEmpty) {
          ans&#43;&#43;;
          desk[i] &#61; 1;
          i&#43;&#43;;
        }
      }
    }

    return ans;
  }
}
</code></pre> 
<h4>JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const desk &#61; line.split(&#34;,&#34;).map(Number);
  console.log(getResult(desk));
});

function getResult(desk) {
  let ans &#61; 0;

  for (let i &#61; 0; i &lt; desk.length; i&#43;&#43;) {
    if (desk[i] &#61;&#61; 0) {
      const isLeftEmpty &#61; i &#61;&#61; 0 || desk[i - 1] &#61;&#61; 0;
      const isRightEmpty &#61; i &#61;&#61; desk.length - 1 || desk[i &#43; 1] &#61;&#61; 0;

      if (isLeftEmpty &amp;&amp; isRightEmpty) {
        ans&#43;&#43;;
        desk[i] &#61; 1;
        i&#43;&#43;;
      }
    }
  }

  return ans;
}
</code></pre> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
desk &#61; list(map(int, input().split(&#34;,&#34;)))


# 算法入口
def getResult():
    ans &#61; 0

    i &#61; 0
    while i &lt; len(desk):
        if desk[i] &#61;&#61; 0:
            isLeftEmpty &#61; i &#61;&#61; 0 or desk[i - 1] &#61;&#61; 0
            isRightEmpty &#61; i &#61;&#61; len(desk) - 1 or desk[i &#43; 1] &#61;&#61; 0

            if isLeftEmpty and isRightEmpty:
                ans &#43;&#61; 1
                desk[i] &#61; 1
                i &#43;&#61; 1
        i &#43;&#61; 1

    return ans


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

#define MAX_SIZE 20000

int getResult(int* desk, int desk_size);

int main()
{
	int desk[MAX_SIZE];
	
	int desk_size &#61; 0;
	while(scanf(&#34;%d&#34;, &amp;desk[desk_size&#43;&#43;])) {
		if(getchar() !&#61; &#39;,&#39;) break;
	}
	
	printf(&#34;%d\n&#34;, getResult(desk, desk_size));
	
	return 0;
}

int getResult(int* desk, int desk_size)
{
	int ans &#61; 0;
	
	for(int i&#61;0; i&lt;desk_size; i&#43;&#43;) {
		if(desk[i] &#61;&#61; 0) {
			int isLeftEmpty &#61; i &#61;&#61; 0 || desk[i-1] &#61;&#61; 0;
			int isRightEmpty &#61; i &#61;&#61; desk_size - 1 || desk[i&#43;1] &#61;&#61; 0;
			
			if(isLeftEmpty &amp;&amp; isRightEmpty) {
				ans&#43;&#43;;
				desk[i] &#61; 1;
				i&#43;&#43;;
			}
		}
	}
	
	return ans;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>